import Layout from '@/layout'

const dynamicRouter = []

const routeFiles = require.context('@/pages', true, /index.vue/);
const pageConfigs = require.context('@/pages', true, /pageConfig.js/);

pageConfigs.keys().forEach((itemConfig, index) => {
    const fileName = routeFiles.keys()[index]

    if(!fileName) return

    const componentConfig = routeFiles(fileName);
    const componentName = fileName.replace(/^\.\//, '').replace(/\.\w+$/, '').replace('/index', '');
    const config = pageConfigs(itemConfig)['default']
    let route = {}

    if (config.hidden) {
        route = {
            path: `/${componentName}`,
            component: componentConfig.default || componentConfig,
            hidden: pageConfigs.keys()[index].hidden
        }
    } else {
        route = {
            path: `/${componentName}`,
            component: Layout,
            children: [{
                path: `/${componentName}`,
                name: config.title,
                component: componentConfig.default || componentConfig,
                meta: { title: config.title, icon: config.icon, requiresAuth: config.requiresAuth }
            }],
        };

    }

    dynamicRouter.push(route);
})


export default dynamicRouter